-- BishopPre
-- Created by cheny3 Sep/08/2016
-- 主教塞缪尔建筑

-- 初始化
function initBishopPre(self)
    if ME.user.bishop then
        -- 已经有主教了，初始化空格
        self:initEmpty();
        return;
    end

    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 使用主教模型
    createBishop(self);
end

-- 创建主教
function createBishop(self)
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            require "game/ui/form/dungeon/special/UIBishopPre"
            UIMgr.getCurrentScene():removeFormByName("UIBishopPre");
            local uiForm = UIBishopPre.create(self.gridData);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end

    if self.monsterNode.bishopDisplay then
        -- 先移除旧模型
        self.monsterNode:removeChild(self.monsterNode.bishopDisplay);
    end

    -- 显示底框
    self.monsterBgNode:setVisible(true);
    local monsterFrame = findChildByName(self.monsterBgNode, "monster_frame");
    monsterFrame:setTouchEnabled(true);
    monsterFrame:setVisible(true);
    monsterFrame:addTouchEventListener(onClicked);

    -- 隐藏血条和攻击
    self.barNode:setVisible(false);
    self.hpBarNode:setVisible(false);

    -- 显示信息
    local function showBishopInfo()
        -- 创建模型
        local bishopDisplay = createBishopDisplay(self);
        self.monsterNode.bishopDisplay = bishopDisplay;
        self.monsterNode:addChild(bishopDisplay);
        self.monsterNode:setVisible(true);
    end
    performWithDelay(self, showBishopInfo, 0.1);
end

-- 创建模型
function createBishopDisplay(self)
    -- 创建模型（用召唤兽的）
    local classId = self.gridData.bishop_id;
    local summonId = BishopM.query(classId, "summon_display")
    local bishopDisplay = SummonDisplay.create(summonId);

    -- 设置缩放比例
    local scale = SummonM.query(summonId, "model_scale");
    bishopDisplay:setScale(scale);

    -- 设置偏移量
    local offsetX = SummonM.query(summonId, "offset_x") or 0;
    local offsetY = SummonM.query(summonId, "offset_y") or 0;

    bishopDisplay:setPosition(tonumber(offsetX), tonumber(offsetY));
    return bishopDisplay;
end